<?php
namespace app\mouth\controller;
use app\mouth\common\Api;
use app\mouth\model\ShopUserModel;
use think\App;
use think\Controller;
use think\Db;
use think\facade\Request;
/**
 *商铺用户信息
 * Class ShopUserApi
 * @package app\mouth\controller
 */
class ShopUserApi extends Controller{
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: token,Origin, X-Requested-With, Content-Type, Accept");
        header('Access-Control-Allow-Methods: POST,GET');
    }

    //订单
    public function order(){
        $this->jsons('40006','成功',ShopUserModel::order((int)input('ma_id'),input('type')));
    }
    //订单详情
    public function detailsorder(){
        $order = ShopUserModel::detailsorder((int)input('or_id'));
        if ($order){
            $this->jsons('40006','成功',$order);
        }
        $this->jsons('40003','订单不存在',[]);
    }

    //指定用户优惠券
    public function appoint(){
        $this->jsons('40006','成功',ShopUserModel::appoint((int)input('ma_id')));
    }
    //指定用户
    public function discountuser(){
        if (empty((int)input('uid')) || empty((int)input('id'))){
            $this->jsons('40003','缺少参数',[]);
        }
       $date = ShopUserModel::discountuser((int)input('uid'),(int)input('id'),input('s_id'),(int)input('state'));
        if ($date){
            return $this->jsons('40006','添加成功',[]);
        }
        return $this->jsons('40003','添加失败',[]);
    }
    //优惠券
    public function discount(){
        $this->jsons('40006','成功',ShopUserModel::discount((int)input('ma_id')));
    }
    //添加优惠券
    public function adddiscount(){
        $date = ShopUserModel::adddiscount(input());
        if ($date === true ){
            $this->jsons('40006','成功',[]);
        }
        $this->jsons('40003','失败',$date);
    }
    //修改优惠券
    public function addupdate(){
        $date = ShopUserModel::addupdate(input());
        if ($date === true ){
            $this->jsons('40006','成功',[]);
        }
        $this->jsons('40003','失败',$date);
    }
    //删除优惠券
    public function adddalete(){
        $date = ShopUserModel::adddalete(input('id'));
        if ($date){
            $this->jsons('40006','成功',[]);
        }
        $this->jsons('40003','失败',[]);
    }
    //添加优惠券 - -车辆信息
    public function bus(){
        $this->jsons('40006','成功',ShopUserModel::bus());
    }

    //基础信息页面
    public function businesspicture(){
        $this->jsons('40006','成功',ShopUserModel::businesspicture((int)input('ma_id')));
    }
    //商家产品
    public function product(){
        $this->jsons('40006','成功',ShopUserModel::product((int)input('ma_id')));
    }


    //上传商铺基础信息
    public function essentialInformation(){
        $ma_id = (int)input('ma_id');
        if (!$ma_id){
            return $this->jsons('40003','缺少重要参数',[]);
        }
        if (!empty($_FILES['file1']['name'])){
            $file = request()->file('file1');//营业执照
            $insert['license'] = $this->add($file);
        }
        if (!empty($_FILES['file2']['name'])){
            $file = request()->file('file2');//道路救援许可证
            $insert['roadrescue'] = $this->add($file);
        }
        if (!empty($_FILES['file3']['name'])){
            $file = request()->file('file3');//报废车辆回收证明
            $insert['certificate'] = $this->add($file);
        }
        if (!isset($insert)){
            return $this->jsons('40003','请上传图片',[]);
        }

       $type = Db::name('store')->where('ma_id',$ma_id)->update($insert);
        if ($type){
            return $this->jsons('40006','上传成功',[]);
        }
        return $this->jsons('40003','上传失败',[]);
    }

    public function add($file){
        $target = $file->rule('md5')->validate(['ext'=>'jpg,png,gif,mp4'])->move( '../public/image',time());
        if ($target == false){
                return $this->jsons('40003','请检查上传内容',[]);
        }
        return "/image/" .$target->getsaveName();
    }
    public function jsons($code, $message = '', $data = array())
    {
        $result = array(
            'code' => $code,
            'message' => $message,
            'data' => $data
        );
        exit(json_encode($result));
    }
}
